﻿using huqiang.Core.HGUI;
using huqiang.Core.Line;
using huqiang.Data;
using huqiang.UIComposite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace huqiang.UIEvent
{
    /// <summary>
    /// 输入框事件
    /// </summary>
    public class InputBoxEvent:UserEvent
    {
        /// <summary>
        /// 输入框复合组件实例
        /// </summary>
        public InputBox input;
        TextBox text;
        /// <summary>
        /// 输入字符串
        /// </summary>
        public string InputString;
        StringEx Content=new StringEx("",false);
        List<TextLineInfo> lines = new List<TextLineInfo>();
        List<CharacterInfoEx> chars = new List<CharacterInfoEx>();
        protected float overDistance = 500;
        protected float overTime = 0;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="mod">数据缓存</param>
        internal override void Initial(FakeStruct mod)
        {
            AutoColor = false;
            text = Context as TextBox;
        }
        public override void OnMouseDown(UserAction action)
        {
            base.OnMouseDown(action);
            input.OnMouseDown(action);
        }
        internal override void OnClick(UserAction action)
        {
            base.OnClick(action);
            input.OnClick(action);
        }
        internal override void OnLostFocus(UserAction action)
        {
            base.OnLostFocus(action);
            input.OnLostFocus(action);
        }
        protected override void OnDrag(UserAction action)
        {
            if (Pressed)
            {
                if (action.CanPosition != action.LastPosition)
                {
                    if (!entry)
                    {
                        float oy = action.CanPosition.y - GlobalPosition.y;
                        float py = GlobalScale.y * text.SizeDelta.y * 0.5f;
                        if (oy > 0)
                            oy -= py;
                        else oy += py;
                        if (oy > overDistance)
                            oy = overDistance;
                        float per = 5000 / oy;
                        if (per < 0)
                            per = -per;
                        overTime += UserAction.TimeSlice;
                        if (overTime >= per)
                        {
                            overTime -= per;
                            if (oy > 0)
                            {
                                text.StartLine--;
                            }
                            else
                            {
                                text.StartLine++;
                                
                            }
                        }
                    }
                    input.OnDrag(action);
                }
            }
            base.OnDrag(action);
        }
        internal override void OnMouseWheel(UserAction action)
        {
            float oy = action.MouseWheelDelta;
            if (oy > 0)
            {
                text.StartLine++;
            }
            else
            {
                text.StartLine--;
            }
            base.OnMouseWheel(action);
        }
        /// <summary>
        /// 设置输入法显示位置,win平台
        /// </summary>
        /// <param name="start">光标按压位置</param>
        public void SetCursorPos(ref PressInfo start)
        {
            if (start.Row < 0)
                return;
            if (start.Row >= lines.Count)
                return;
            int index = text.GetIndex(ref start);
            if (index < 0)
                return;
            if (index >= chars.Count)
                return;
            int row = start.Row;
            for (int i = 0; i < lines.Count; i++)
            {
                if (index <= lines[i].CharEnd)
                {
                    row = i;
                    break;
                }
            }
            float p;
            if (index > lines[row].CharEnd)
            {
                index--;
                if (index < 0)
                    index = 0;
                p = chars[index].Right;
            }
            else
            {
                p = chars[index].Left;
            }
            float right = p + 1;
            float down = lines[row].downY;
            var gl = UIElement.GetGlobaInfo(text.transform, false);
            float rx = gl.Scale.x * right;
            float rd = gl.Scale.y * down;
            gl.Postion.x += rx;
            gl.Postion.y += rd;
            gl.Postion *= HCanvas.MainCanvas.PhysicalScale;
            gl.Postion.x += Screen.width / 2;
            gl.Postion.y += Screen.height / 2;
            Keyboard.CursorPos = new Vector2(gl.Postion.x, Screen.height - gl.Postion.y);
        }
    }
}
